package com.controller;

import com.service.orderService;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class showUserOrderInfoServlet extends HttpServlet {
    private orderService orderService=new orderService();
    private Set<String> set=new HashSet<>();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String uid=null;
        Cookie[] cookies = req.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            if(cookies[i].getName().equals("loginUser")){
                uid=   cookies[i].getValue();
                break;
            }
        }
        if(uid!=null){
            try {
                ResultSet resultSet = orderService.queryAllOrder(Integer.parseInt(uid));

                //筛选订单编号
                while (resultSet.next()){
                    set.add(resultSet.getString(1));
                }
                Object[] objects = set.toArray(); //存储编号的Object数组
                for (int i = 0; i < objects.length; i++) {
                    out.print("<a href=\"OrderInfoServlet?od="+objects[i]+"&us="+uid+    "\">订单号:"+objects[i]+"的信息</a><br/>");
                }




            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }




        }








    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
